Passed
Pull Request — master (#50)
by Pieter Epeüs
08:42
created

objects.js ➔ parse   A

Complexity

Conditions 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 9.85
c 0
b 0
f 0
cc 4
1
/**
2
 * Object helper
3
 */
4
module.exports = class Obj {
5
    /**
6
     * Set the original and prefix.
7
     *
8
     * @param {object} original
9
     * @param {string} prefix
10
     *
11
     * @return {object}
12
     */
13
    constructor(original, prefix) {
14
        this.original = original;
15
        this.prefix = prefix;
16
        this.flatObject = {};
17
        this.parse();
18
19
        return this;
0 ignored issues
show
Bug introduced by
The constructor does not have a meaningful return value. Are you sure this is correct?
Loading history...
20
    }
21
22
    /**
23
     * flatten the object 1 level per time.
24
     */
25
    parse() {
26
        Object.entries(this.original).forEach(
27
            ([originalRowIndex, originalRow]) => {
28
                let index = originalRowIndex;
29
30
                if (this.prefix) {
31
                    index = [this.prefix, originalRowIndex].join('.');
32
                }
33
34
                if (typeof originalRow === 'object') {
35
                    const childRows = new Obj(originalRow, index).flat;
36
37
                    this.flatObject = Object.assign(this.flatObject, childRows);
38
39
                    return;
40
                }
41
42
                this.flatObject[index] = originalRow;
43
            }
44
        );
45
    }
46
47
    /**
48
     * Get the flat object.
49
     *
50
     * @return {object}
51
     */
52
    get flat() {
53
        return this.flatObject;
54
    }
55
56
    /**
57
     * Get the object entries.
58
     *
59
     * @return {array}
60
     */
61
    entries() {
62
        return Object.entries(this.flatObject);
63
    }
64
65
    /**
66
     * Get the object keys.
67
     *
68
     * @return {array}
69
     */
70
    keys() {
71
        return Object.keys(this.flatObject);
72
    }
73
74
    /**
75
     * Get the object values.
76
     *
77
     * @return {array}
78
     */
79
    values() {
80
        return Object.values(this.flatObject);
81
    }
82
83
    /**
84
     * Get the object length.
85
     *
86
     * @return {number}
87
     */
88
    get length() {
89
        return Object.keys(this.flatObject).length;
90
    }
91
92
    /**
93
     * Get an item by key.
94
     *
95
     * @param {string} key
96
     * @param {object|null} defaultValue
97
     *
98
     * @return {object|null}
99
     */
100
    getByKey(key, defaultValue) {
101
        if (this.has(key)) {
102
            return this.flatObject[key];
103
        }
104
105
        if (this.includes(key)) {
106
            return this.entries()
107
                .filter(([currentKey]) => currentKey.startsWith(key))
108
                .reduce((accumulator, [currentKey, currentValue]) => {
109
                    const subKey = currentKey.substring(key.length + 1);
110
                    accumulator[subKey] = currentValue;
111
                    return accumulator;
112
                }, {});
113
        }
114
115
        return defaultValue;
116
    }
117
118
    /**
119
     * Get keys of an item.
120
     *
121
     * @param {array} keys
122
     * @param {object|null} defaultValue
123
     *
124
     * @return {object|null}
125
     */
126
    getKeys(keys, defaultValue) {
127
        const result = this.entries().filter(([currentKey]) =>
128
            keys.some(key => currentKey.startsWith(key))
129
        );
130
131
        if (result && result.length > 0) {
132
            return result.reduce((accumulator, [currentKey, currentValue]) => {
133
                accumulator[currentKey] = currentValue;
134
                return accumulator;
135
            }, {});
136
        }
137
138
        return defaultValue;
139
    }
140
141
    /**
142
     * Check if the object has a key.
143
     *
144
     * @param {string} key
145
     *
146
     * @return {boolean}
147
     */
148
    has(key) {
149
        return Object.prototype.hasOwnProperty.call(this.flatObject, key);
150
    }
151
152
    /**
153
     * Check if the object has a key that includes.
154
     *
155
     * @param {string} key
156
     *
157
     * @return {boolean}
158
     */
159
    includes(key) {
160
        return this.keys().filter(item => item.startsWith(key)).length > 0;
161
    }
162
};
163